<?php
    
    namespace App\Observers;

    use App\System\Base\Repositories\BaseNotificationRecordRepository;
    use App\System\Cis\Repositories\CisUserRepository;
    use App\System\Fina\Model\FinaIntegralRegRecord;
    use App\System\Log\Repositories\LogAdminRepository;

    class FinaIntegralRegRecordObserver
    {

        /**
         * 监听更新的事件。
         *
         * @param  FinaIntegralRegRecord  $finaIntegralRegRecord
         * @return void
         */
        public function updated(FinaIntegralRegRecord $finaIntegralRegRecord)
        {
            if($finaIntegralRegRecord->status == 1)
            {
                $this->addNotification($finaIntegralRegRecord);
            }
            $data['type'] = 1;
            $data['log_info'] = '修改财务信息';
            if($finaIntegralRegRecord->isDirty('status')){

                if($finaIntegralRegRecord->type == 6 && $finaIntegralRegRecord->status==2){
                    if($finaIntegralRegRecord->record_type==1){
                        $data['log_info'] = '审核积分不发放';
                    }else{
                        $data['log_info'] = '审核注册币不发放';
                    }
                }

                if($finaIntegralRegRecord->type == 7){
                    $data['log_info'] = '处理用户提现';
                }
                if($finaIntegralRegRecord->type == 3 || $finaIntegralRegRecord->type == 4){
                    $data['log_info'] = $finaIntegralRegRecord->type==3?$finaIntegralRegRecord->user_id.' 转出注册币':$finaIntegralRegRecord->user_id.' 转入注册币';
                }
            }
            $logAdmin = LogAdminRepository::getInstance();
            $logAdmin->saveAdminLog($data);

        }

        public function created(FinaIntegralRegRecord $finaIntegralRegRecord)
        {
            if($finaIntegralRegRecord->status == 1)
            {
                $this->addNotification($finaIntegralRegRecord);
            }
        }

        private function addNotification($finaIntegralRegRecord)
        {
            $notification = BaseNotificationRecordRepository::getInstance();
            $user = CisUserRepository::getInstance()->getUser($finaIntegralRegRecord->user_id);
            $title = [1=>'积分变动',2=>'注册币变动'];
            $set = ['title'=>$title[$finaIntegralRegRecord->record_type],'content'=>$finaIntegralRegRecord->remark_info,'type'=>1,'from_id'=>$finaIntegralRegRecord->id,'user_id'=>$user->parent_id];
            $notification->addData($set);
        }
    }
     
    